package com.eden.geoedu.view;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import com.eden.geoedu.HighScoreBean;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HighScoreDialog extends Dialog {

	private String map_name;
	private HighScoreBean[] scoreTab;
	final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.ENGLISH);
	DecimalFormat df = new DecimalFormat("#,###", dfs);
	private View view;

	public HighScoreDialog(Context context, String map,
			HighScoreBean[] scoreList, View view) {
		super(context);
		this.map_name = map;
		this.scoreTab = scoreList;
		this.view = view;

		dfs.setGroupingSeparator(' ');
		df = new DecimalFormat("#,###", dfs);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.highscore);
		setTitle(" Best scores - " + map_name);

		final TextView nameView1 = (TextView) findViewById(R.id.name1);
		final TextView scoreView1 = (TextView) findViewById(R.id.score1);
		final TextView nameView2 = (TextView) findViewById(R.id.name2);
		final TextView scoreView2 = (TextView) findViewById(R.id.score2);
		final TextView nameView3 = (TextView) findViewById(R.id.name3);
		final TextView scoreView3 = (TextView) findViewById(R.id.score3);
		final TextView nameView4 = (TextView) findViewById(R.id.name4);
		final TextView scoreView4 = (TextView) findViewById(R.id.score4);
		final TextView nameView5 = (TextView) findViewById(R.id.name5);
		final TextView scoreView5 = (TextView) findViewById(R.id.score5);
		final TextView nameView6 = (TextView) findViewById(R.id.name6);
		final TextView scoreView6 = (TextView) findViewById(R.id.score6);

		TextView[] nameViewTab = new TextView[] { nameView1, nameView2,
				nameView3, nameView4, nameView5, nameView6 };
		TextView[] scoreViewTab = new TextView[] { scoreView1, scoreView2,
				scoreView3, scoreView4, scoreView5, scoreView6 };

		final Button buttonOK = (Button) findViewById(R.id.buttonOK);
		buttonOK.setOnClickListener(new OKListener());

		for (int i = 0; i < scoreTab.length; i++) {
			if (scoreTab[i] !=null) {
				nameViewTab[i].setText(scoreTab[i].getUserName());
				scoreViewTab[i].setText(df.format(scoreTab[i].getScore()));
			}
		}

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			view.setEnabled(true);

			return super.onKeyDown(keyCode, event);
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	private class OKListener implements android.view.View.OnClickListener {

		@Override
		public void onClick(View v) {
			view.setEnabled(true);
			HighScoreDialog.this.dismiss();
		}
	}

}